package cn.accp.mvc.controller;



import java.io.IOException;
import java.sql.Date;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;


import cn.accp.mvc.entity.House;
import cn.accp.mvc.entity.Users;
import cn.accp.mvc.entity.allEntity;
import cn.accp.mvc.service.HouseService;

@Controller
@RequestMapping("/page")
public class ActionController {
	@Autowired
	private HouseService houseService;
	public void setHouseService(HouseService houseService) {
		this.houseService = houseService;
	}
	@RequestMapping("/delete")
	public String delete(String id,HttpSession session){
		boolean isOK = false;	
		isOK = houseService.delete(Integer.parseInt(id));
		if(isOK){
			session.setAttribute("opr", "guanli");
			return "/page/gethouseList.do";
		}else {
			return "/page/guanli.jsp";
		}
	}
	@RequestMapping("/update")
	public String update(String id,String opr,HttpSession session){
		allEntity  house = houseService.getHouse(Integer.parseInt(id));
		session.setAttribute("house", house);
		session.setAttribute("opr", opr);
		return "/page/getAllTypes.do";
	}
	@RequestMapping("/saveORupdate")
	public String fabu(String id,String title,String tid,String floorage,String price,String pubdate,String sid,String contact,String description,String oprType,HttpSession session,HttpServletResponse response) throws IOException{
		boolean isOK = false;	
		House house = null;
		Users users = (Users)session.getAttribute("users");
		int uid = (int)users.getId();
		house = new House(title,description,Double.parseDouble(price),Date.valueOf(pubdate),Double.parseDouble(floorage),contact,uid,Integer.parseInt(sid),Integer.parseInt(tid));
		if(oprType.equals("立即发布")){
			isOK = houseService.save(house);
		}else {
			house.setId(Integer.parseInt(id));
			isOK = houseService.update(house);
		}	
		if(isOK){
			session.setAttribute("opr", "guanli");
			if(oprType.equals("立即发布")){
				return "/page/gethouseList.do";
			} else{
				return "/page/gethouseList.do";
			}			
		}else {
			return "/page/fabu.jsp";
		}
	}
//	@RequestMapping("testAlert")
//	public void alert(HttpServletResponse response) throws IOException{
//		System.out.println("ActionController.alert()");
//		response.setCharacterEncoding("UTF-8");
//		response.getWriter().println("<script>alert('登录成功！');location.href='/SSM_house/page/login.do'</script>");
//	}
}
